﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class AnalizServisBakim : DevExpress.XtraEditors.XtraForm
    {
        public static DataTable ds;
        String Sorgu = "";
        public static string Bilgi = "Filtreleri kullanarak çok boyutlu analiz için raporu düzenleyebilirsiniz. ";
        
        public AnalizServisBakim()
        {
            InitializeComponent();
        }

        private void _btnKapat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void AnalizServisBakim_Load(object sender, EventArgs e)
        {
            RaporIcinGrup.Text = "(Servis Bakım Analizi)";

            progress.MarqueeAnimationSpeed = 25;
            progress.Stopped = true;

            RaporGridView.ShowCustomization();
            RaporGridView.CustomizationForm.Location = new System.Drawing.Point((RaporGrid.Location.X + RaporGrid.Width) - 10, (RaporGrid.Location.Y + RaporGrid.Height) - 50);

            try
            {
                ds.Rows.Clear();
            }
            catch (Exception)
            {
            }

            BWServisBakim.RunWorkerAsync();
        }

        private void _btnZimmetliEnvanter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _btnZimmetliEnvanter.Enabled = false;
            RaporIcinGrup.Text = Bilgi + "(Servis Bakım Analizi)";
            BWServisBakim.RunWorkerAsync();
            progress.Stopped = false;
            _btnZimmetliEnvanter.Enabled = true;
        }

        private void _btnExceleAktar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (ds.Rows.Count > 0)
                {
                    KaydetDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    KaydetDialog.FileName = "";
                    KaydetDialog.Filter = "Excel (*.xlsx)|*.xlsx";
                    KaydetDialog.FilterIndex = 1;
                    KaydetDialog.RestoreDirectory = true;

                    if (KaydetDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        RaporGridView.ExportToXlsx(KaydetDialog.FileName);
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void _btnYazdir_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (ds.Rows.Count > 0) RaporGridView.ShowPrintPreview();
            }
            catch (Exception)
            {
            }
        }

        private void _btnPdfOlarakKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (ds.Rows.Count > 0)
                {
                    KaydetDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    KaydetDialog.FileName = "";
                    KaydetDialog.Filter = "Pdf Dosyası (*.pdf)|*.pdf";
                    KaydetDialog.FilterIndex = 1;
                    KaydetDialog.RestoreDirectory = true;

                    if (KaydetDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        RaporGridView.ExportToPdf(KaydetDialog.FileName);
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void BWServisBakim_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                String Sorgu = "SELECT \n" +
             "dbo.ServisFormlari.id [ID], \n" +
             "(CASE WHEN dbo.ServisFormlari.EvrakCinsi = 1 THEN 'KABUL FORMU' ELSE 'SERVİS SEVK FORMU' END) [EVRAK CİNSİ], \n" +
             "dbo.Tedarikci.tFirmaUnvan [SERVİS], \n" +
             "dbo.ServisFormlari.aciklama [AÇIKLAMA], \n" +
             "FORMAT(dbo.ServisFormlari.dto_evrak,'dd.MM.yyyy')[Evrak Tarihi], \n" +
             "dbo.CihazTur.ChzTur [CİHAZ CİNSİ], \n" +
             "dbo.CihazMarka.ChzMarka [MARKA], \n" +
             "dbo.CihazModel.ChzModel [MODEL], \n" +
             "dbo.Cihaz.SeriNoIMEI [SERİ NO/IMEI], \n" +
             "(SELECT tFirmaUnvan FROM Tedarikci where tID=id_tedarikci_kargo)[KARGO], \n" +
              "dbo.ServisFormlari.KargoTakipKodu [KARGO TAKİP KODU] \n" +
             "FROM dbo.ServisFormlari LEFT OUTER JOIN \n" +
             "dbo.Tedarikci ON dbo.ServisFormlari.id_tedarikci = dbo.Tedarikci.tID LEFT OUTER JOIN \n" +
             "dbo.CihazTur INNER JOIN \n" +
             "dbo.Cihaz ON dbo.CihazTur.ctID = dbo.Cihaz.ctID INNER JOIN \n" +
             "dbo.CihazMarka ON dbo.Cihaz.cmID = dbo.CihazMarka.cmID INNER JOIN \n" +
             "dbo.CihazModel ON dbo.Cihaz.cmdID = dbo.CihazModel.cmdID ON dbo.ServisFormlari.id_cihaz = dbo.Cihaz.cID \n" +
             "WHERE (dbo.ServisFormlari.isDeleted = 0)";

                ds = Army.SQL().GetDataTable(Sorgu);
            }
            catch (Exception)
            {
            }
        }

        private void BWServisBakim_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                if (ds.Rows.Count > 0)
                {
                    RaporGridView.Columns.Clear();
                    RaporGrid.DataSource = ds;
                    RaporGridView.GroupSummary.Clear();
                    //RaporGridView.GroupFooterShowMode = GroupFooterShowMode.VisibleAlways;

                    //RaporGridView.Columns["ENVANTER GİRİŞ TARİHİ"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                    //RaporGridView.Columns["ENVANTER GİRİŞ TARİHİ"].DisplayFormat.FormatString = "{0:dd/MM/yyyy}";
                    //RaporGridView.Columns["ZİMMET EVRAK TARİHİ"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                    //RaporGridView.Columns["ZİMMET EVRAK TARİHİ"].DisplayFormat.FormatString = "{0:dd/MM/yyyy}";

                    //RaporGridView.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "ENVANTER CİNSİ");
                    //RaporGridView.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "ENVANTER CİNSİ", RaporGridView.Columns["ENVANTER CİNSİ"]);
                    //RaporGridView.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "MALİYET DEĞERİ", RaporGridView.Columns["MALİYET DEĞERİ"], "Toplam {0:n2}");
                    //RaporGridView.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "ZİMMET DEĞERİ", RaporGridView.Columns["ZİMMET DEĞERİ"], "Toplam {0:n2}");

                    //RaporGridView.Columns["ENVANTER CİNSİ"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
                    //RaporGridView.Columns["ENVANTER CİNSİ"].SummaryItem.DisplayFormat = "Toplam = {0:n}";
                    //RaporGridView.Columns["MALİYET DEĞERİ"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                    //RaporGridView.Columns["MALİYET DEĞERİ"].SummaryItem.DisplayFormat = "Toplam = {0:n}";
                    //RaporGridView.Columns["ZİMMET DEĞERİ"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                    //RaporGridView.Columns["ZİMMET DEĞERİ"].SummaryItem.DisplayFormat = "Toplam = {0:n}";

                    //RaporGridView.Columns["AÇIKLAMA"].Visible = false;
                    //RaporGridView.Columns["ZİMMET AÇIKLAMASI"].Visible = false;
                    //RaporGridView.Columns["DEPARTMAN"].Visible = false;
                    //RaporGridView.Columns["GÖREV/ÜNVAN"].Visible = false;
                    //RaporGridView.Columns["SERVİS TAG"].Visible = false;
                    //RaporGridView.Columns["ENVANTER GİRİŞ TARİHİ"].Visible = false;
                    //RaporGridView.Columns["TEDARİKÇİ"].Visible = false;
                    //RaporGridView.Columns["MALİYET DEĞERİ"].Visible = false;
                    //RaporGridView.Columns["MALİYET DÖVİZ"].Visible = false;

                    foreach (DevExpress.XtraGrid.Columns.GridColumn x in RaporGridView.Columns)
                    {
                        x.OptionsFilter.FilterPopupMode = DevExpress.XtraGrid.Columns.FilterPopupMode.CheckedList;
                        x.OptionsColumn.AllowIncrementalSearch = true;
                        x.OptionsColumn.AllowEdit = false;
                    }

                    RaporGridView.BestFitColumns();
                }
            }
            catch (Exception)
            {

            }
            progress.Stopped = true;
        }
    }
}